package org.wmine.core;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 * 
 * General Input Dialog Class
 * @author  Prafulla
 * 
 */
public class InputDialog extends JDialog {

	private String savedInputString;
	private JLabel errorLabel;
	private JTextField inputTextField;
	
	public InputDialog(Frame parentComponent, String title, String message) {
		super(parentComponent,true);
		setTitle(title);
		Container container = getContentPane();
		setLayout(new GridLayout(3,1));

	
		JPanel inputPanel = new JPanel();
		JLabel inputLable = new JLabel(message);
		inputTextField = new JTextField(15);
		inputLable.setLabelFor(inputTextField);
		inputPanel.add(inputLable);
		inputPanel.add(inputTextField);
		
		JButton okButton = new JButton("    OK    ");
		JButton cancelButton = new JButton("  Cancel  ");
		JPanel actionPanel = new JPanel();
		actionPanel.add(okButton);
		actionPanel.add(cancelButton);

		
		JPanel errorPanel = new JPanel();
		errorLabel = new JLabel("Error :   ");
		errorPanel.add(errorLabel);
		
		
		container.add(inputPanel);
		container.add(actionPanel);
		container.add(errorPanel);
		
		
		pack();
		setSize(400,174);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		TODO: Please try to find how the dialog can be shown in the center
//				Consider parentComponent as the parent component, and try to show thid dialog box 
//				in the center of the this component
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				if(validateInput()) {
					disposeFrame();
				}
				else
					setErrorLabel();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
					disposeFrame();
			}

		});
		setLocation(40,50);
		setVisible(true);
	}
	
	public void showInputDialog() {
	
	}
	

	public void disposeFrame() {
		this.setVisible(false);
	}
	
	/**
	 *  @return Input entered By user 
	 *
	 **/
	public String getInputString() {
		String returnedString = null;
		try {
			returnedString = inputTextField.getText();
		}
		catch(Exception e) {
			returnedString = savedInputString;
		}
		return returnedString;
	}
	

	protected boolean validateInput() {
		return false;
	}
	
	private void setErrorLabel() {
		String errorMessage = getErrorText();
		if(errorMessage != null)
			this.errorLabel.setText("Error : "+errorMessage);
	}
	
	/**
	 *  Do Nothing--Let Subclass decide what to do!!! 
	 **/
	protected void processInput() {
		return;
		
	}
	
	protected String getErrorText() {
		return "No Error";
	}
	
	public static void main(String[] args) {
		InputDialog inputDialog = new InputDialog(null,"Message","Enter something :");
		System.out.println("DFD");
		System.out.println("DFD");
	}
}

